﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thor.Common;
using Thor.Configuration;
using Thor.Domain;

namespace Thor.Repository.EntFx
{
    public class RepositoryBase : IRepository
    {
        #region Constants

        private const string APPLICATIONNAME = "ApplicationName";
        private const string CONNECTIONSTRING = "ConnectionString";

        #endregion

        #region Fields

        private Model _model = null;
        private string _connectionString = string.Empty;

        #endregion

        #region Constructors

        public RepositoryBase(Dictionary<string, object> repositoryParameters)
        {
            RepositoryParameters = repositoryParameters;
        }

        #endregion

        #region Properties

        public string ApplicationName
        {
            get
            {
                if (RepositoryParameters[APPLICATIONNAME] != null)
                    return Convert.ToString(RepositoryParameters[APPLICATIONNAME]);
                else
                    return string.Empty;
            }
            set
            {
                if (RepositoryParameters[APPLICATIONNAME] != null)
                    RepositoryParameters[APPLICATIONNAME] = value;
            }
        }

        public string ConnectionString
        {
            get
            {
                if (string.IsNullOrEmpty(_connectionString))
                {
                    ConfigurationSettings settings = new ConfigurationSettings();
                    _connectionString = settings.DataConnectionString;
                }

                return _connectionString;
            }
        }

        public Model Model
        {
            get
            {
                if (_model == null)
                    _model = new Model(ConnectionString);

                return _model;
            }
        }

        #endregion

        #region IRepository Members

        public Dictionary<string, object> RepositoryParameters { get; set; }

        #endregion
    }
}
